﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FourKill.Engine
{
    public class Mist : IPaintable, IDisposable
    {
        private static Mist _instance;
        public static Mist Instance { get { _instance = _instance ?? new Mist(); return _instance; } }
        private Brush _brush = new SolidBrush(Color.FromArgb(6, 240, 240, 240));
        private Bitmap _mistImage = null;

        public Mist() { }

        public void Paint(IPaintableContext context)
        {
            try
            {
                if (_mistImage == null)
                {
                    _mistImage = new Bitmap(1, 50);
                    _mistImage.MakeTransparent();
                    using (Graphics g = Graphics.FromImage(_mistImage))
                    {
                        for (int i = 4; i < 50; i += 2)
                        {
                            g.FillRectangle(_brush, new Rectangle(0, ((50 - i) / 2), 1, i));
                        }
                    }
                }
                int viewWidth = context.ViewPort.PixelSize.Width,
                    viewHeight = context.ViewPort.PixelSize.Height,
                    mistHeight = Convert.ToInt32(context.ViewPort.PixelSize.Height * ((MistObject)context.GameObject).Percentage);

                Rectangle rect = new Rectangle(0, ((viewHeight - mistHeight) / 2), viewWidth, mistHeight);

                context.FrontLayer.DrawImageUnscaled(
                    Utility.ResizeImage(
                        rect.Size.Width,
                        rect.Size.Height, _mistImage),
                        rect);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("***********************************");
                System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
                System.Diagnostics.Debugger.Break();
            }
        }


        public void Dispose()
        {
            _brush.Dispose();
        }
    }
}
